package org.example.lp.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.lp.config.MQConstance;
import org.example.lp.service.IDemoService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Objects;

@Service
public class DemoService implements IDemoService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void mqSendDemo(Long id, String username) {
        HashMap<String, Object> map = new HashMap<>();
        id = Integer.MAX_VALUE * 2L;
        map.put("id", id);
        map.put("username", username);
        String msg = null;
        try {
             msg = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        rabbitTemplate.convertAndSend(MQConstance.DEMO_DIRECT_EXCHANGE,
                MQConstance.DEMO_QUEUE, msg);
    }

}
